<?php

!function_exists('adminmsg') && exit('Forbidden');
require_once(R_P . 'require/class_resource.php');

new ResourceControl();


class ResourceControl
{
	public function __construct()
	{
		if (isset($_GET['action']) || isset($_POST['action']))
		{
			$action = isset($_GET['action']) ? 'get' . $_GET['action'] : 'get' . $_POST['action'];
			$this->$action();
		}
		else
		{
			$this->getIndex();
		}
	}

	protected function getIndex()
	{
		global $very, $restype, $admin_file, $basename;
		$type = $_GET['type'];
		if (empty($type))
		{
			$typename = '所有资源';
		}
		else 
		{
			$typename = $restype[$type]['typename']; ;
		}
		$res = new Resource();
		$res->setType($type);
		
		global $method, $minsize, $maxsize, $keyword;
		
		$res->setWhere('r.delflag = 0');
		if ($method == 'search')
		{
			$urladd = 'method=search&';
			if (!empty($minsize))
			{
				$urladd .= "minsize=$minsize&";
				$minsize = $minsize*1000000;
				$res->setWhere('r.size > ' . $minsize);
			}
			if (!empty($maxsize))
			{
				$urladd .= "maxsize=$maxsize&";
				$maxsize = $maxsize*1000000;
				$res->setWhere('r.size < ' . $maxsize);
			}
			if (!empty($keyword))
			{
				$urladd .= "keyword=$keyword&";
				$res->setWhere("(r.filetitle LIKE('%$keyword%') OR c.title LIKE('%$keyword%'))");
			}
		}
		$count = $res->getListNum();
		
		$page = intval($_GET['page']);
		$pagesize = 20;
		$numofpage = ceil($count/$pagesize);
		$files = $res->getList($page, $pagesize);
		
		$pages = numofpage($count, $page, $numofpage, "$admin_file?adminjob=resource&type=$type&$urladd");
		
		require PrintEot('header');
		require PrintEot('resource');
		adminbottom();
	}
	
	protected function getDel()
	{
		require_once(R_P . 'require/class_resource.php');
		$res = new Resource();
		foreach ($_POST['del'] as $tid => $rids)
		{
			$res->remove($rids, $tid);
		}
		adminmsg('成功删除文件');
	}
}
?>